package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    private Connection conn;
    //根据用户编号与密码查询用户信息
    @Override
    public User squery(String userId, String password) {
        User user = new User();
//        conn = DBUtil.getConnection();
        try {
            user = userDao.query(userId,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return user;
    }
//根据用户编号查询用户表返回的行数
    @Override
    public int squeryrow(String userId) {
        int a = 0;
//        conn = DBUtil.getConnection();
        try {
            a = userDao.queryrow(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return a;
    }
//向用户表中添加一条记录
    @Override
    public int suinsert(User user) {
        int a = 0;
//        conn = DBUtil.getConnection();
        try {
            DBUtil.beginTransaction();
            a = userDao.uinsert(user);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        return a;
    }
}
